package com.yunwrite.playerlibrary.customplayer.view;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Toast;


import com.yunwrite.playerlibrary.R;
import com.yunwrite.playerlibrary.customplayer.playmanager.PlayManager;
import com.yunwrite.playerlibrary.customplayer.videolist.VideoListManager;

/**
 * 播放器的自定义控件，可以设置播放地址的列表，然后顺序，无间断播放，
 *
 * @author liuping
 */
public class CustomPlayerView extends FrameLayout implements View.OnClickListener{
    /**
     * 上下文
     */
    Context context;
    /**
     * 进度条
     */
    SeekBar seekBar;
    /**
     * 界面
     */
    TextureView textureView;
    Button btnNext;
//    Button btnPre;
//    Button btnBack;
//    Button btnForword;
    LinearLayout player_control;

    /**
     * 节目单管理器
     */
    VideoListManager videoListManager;
    /**
     * 播放控制器
     */
    PlayManager playManager;
    public CustomPlayerView(Context context) {
        super(context);
        initView(context,null,-1);
    }
    public CustomPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs,-1);
    }
    public CustomPlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs,defStyleAttr);
    }
    private void initView(final Context context, AttributeSet attrs, int defStyleAttr) {
        this.context=context;
        //初始化布局
        View view = LayoutInflater.from(context).inflate(R.layout.player_view, this);
        seekBar= view.findViewById(R.id.seek_bar);
        textureView= view.findViewById(R.id.texture_view);
        player_control=view.findViewById(R.id.player_control);
        btnNext=view.findViewById(R.id.next);
        btnNext.setOnClickListener(this);
//        btnPre=view.findViewById(R.id.pre);
//        btnForword=view.findViewById(R.id.forword);
//        btnBack=view.findViewById(R.id.back);
        //初始化歌单
        videoListManager=VideoListManager.getInstance();
        playManager= PlayManager.getInstance();
        textureView.setVisibility(View.VISIBLE);
        textureView.setSurfaceTextureListener(new TextureListener( ) {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                //textureView初始化完成，得到surface对象,可以接受播放器的输出了。
                playManager.setSurface(new Surface(surface));
                playManager.setVideoListManager(videoListManager);
            }
        });
        playManager.setSeekBar(seekBar);
    }
    /**
     * 获取歌单管理类的实例
     * @return
     */
    public VideoListManager getVideoListManager() {
        return videoListManager;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {

        int i = v.getId( );
        if (i == R.id.next) {
            playManager.next();
        }
    }
}